//
//  MetaTagController.m
//  iGSS
//
//  Copyright 2010 Electronic Business Systems Ltd.
//
//  Licensed under the Apache License, Version 2.0 (the "License");
//  you may not use this file except in compliance with the License.
//  You may obtain a copy of the License at
//
//  http://www.apache.org/licenses/LICENSE-2.0
//
//  Unless required by applicable law or agreed to in writing, software
//  distributed under the License is distributed on an "AS IS" BASIS,
//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//  See the License for the specific language governing permissions and
//  limitations under the License.
//

#import "MetaTagController.h"
#import "FormattingUtilities.h"
#import "GSSCommunicator.h"
#import "GSSObject.h"

@implementation MetaTagController

- (NSInteger)numberOfRows {
    NSInteger count = [object.tags count];
    return self.isEditing ? count + 1 : count;
}

- (UITableViewCell *)cellForRow:(NSInteger)row {
    static NSString *CellIdentifier = @"MetaTagCell";
    

    UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (!cell) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2
                                       reuseIdentifier:CellIdentifier] autorelease];
        cell.detailTextLabel.adjustsFontSizeToFitWidth = YES;
        cell.detailTextLabel.minimumFontSize = 11;
    }
    
    if (row < [object.tags count]) {
        cell.detailTextLabel.text = [object.tags objectAtIndex:row];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.editingAccessoryType = UITableViewCellAccessoryNone;
    } else {
        cell.detailTextLabel.text = LocalizedString(@"Add tag");
        cell.selectionStyle = UITableViewCellSelectionStyleBlue;
        cell.editingAccessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    
    return cell;
}

- (NSString *)titleForHeader {
    if ([object.tags count] || self.isEditing)
        return LocalizedString(@"Tags");
    else
        return nil;
}

- (UITableViewCellEditingStyle)editingStyleForRow:(NSInteger)row {
    if (row < [object.tags count])
        return UITableViewCellEditingStyleDelete;
    else
        return UITableViewCellEditingStyleInsert;
}

- (BOOL)shouldIndentWhileEditingRow:(NSInteger)row {
    return YES;
}

- (void)didSelectRow:(NSInteger)row {
    if (!self.isEditing || row < [object.tags count])
        return;
    
	AddTagController *addTagController = [[AddTagController alloc] initWithNibName:@"AddTagController"
                                                                            bundle:nil];
	addTagController.delegate = self;
	[self.navigationController pushViewController:addTagController animated:YES];
	[addTagController release];    
}

- (void)commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRow:(NSInteger)row {
    NSArray *tags = object.tags;
    NSString *tag = [tags objectAtIndex:row];

    NSMutableArray *m = [NSMutableArray arrayWithArray:tags];
    [m removeObject:tag];
    NSArray *newTags = [NSArray arrayWithArray:m];
    [[GSSCommunicator defaultCommunicator] setTags:newTags ofObject:object];
    object.tags = newTags;
    [object save];
    
    [self.tableView reloadData];
}

//MARK: -
//MARK: AddTagDelegate

- (void)tagAdded:(NSString *)tag {
    NSArray *tags = object.tags;
    if (!tag || [tags containsObject:tag])
        return;

    NSArray *newTags = [tags arrayByAddingObject:tag];
    [[GSSCommunicator defaultCommunicator] setTags:newTags ofObject:object];
    object.tags = newTags;
    [object save];
}

@end
